package com.knowledge.share.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.DigestUtils;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Slf4j
public class EncryptUtils {
    //公私钥内容
    static private String privateKey ="MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA3G/OsVV/wXQRIFd7PhD9G3d8DHIcGWuDje4vnmtbpVrvRodLiqFQiY6imJfKJ4PzV7gToIEcTfhvW7aOrctrzwIDAQABAkB5In/k92UDO/MFYM165/8PDvP+Kt7CIVDjt9Y3Kivsiyy0GAebsRIcAck8YaCeCxLM3rqcGB66BQzKxIgNGmPxAiEA+cMKSLmBbJ9216SFPxDvMfSrws1VSulzYrI8RyVslqUCIQDh8UOxAHYaCz88OxfIPI5aFrx6fPhcpOihsmkZjvpiYwIhAPbXt5tGk85KEAdyFyrMTsrdmL/vdcOS1DIQG5jpONftAiEAmiIgb4tFBhdOGCSn4K2O3t5LzbObKnhFSqmPESR8oskCIQCKH3EG5n9iu+c9jE5IxX+LJww/JUVaKvIjWzJTqqwiJA==";
    private static String publicKey="MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANxvzrFVf8F0ESBXez4Q/Rt3fAxyHBlrg43uL55rW6Va70aHS4qhUImOopiXyieD81e4E6CBHE34b1u2jq3La88CAwEAAQ==";


    /**
     * @desc 密码加密
     * @param str
     * @return
     * @throws Exception
     */
    public static String encrypt(String str) throws Exception{
        // base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        // RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
    }
    /**
     * @desc 解密
     * @param encryptData
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptData) throws Exception{
        // 64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(encryptData.getBytes("UTF-8"));
        // base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        // RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        return new String(cipher.doFinal(inputByte));
    }


    /**
     * 生成字符串md5加密的密码内容, 使用springboot框架处理
     * @param pwd
     * @return
     */
    public static String generateMd5(String pwd){
        //使用spring的工具完成加密的操作
        String md5Pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());

        log.info("加密后的密码内容是:{}",md5Pwd);
        return md5Pwd;
    }

    /**
     * @desc 使用原生java对象来创建md5密码信息。
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String createMd5(String str) throws NoSuchAlgorithmException {
        //原生Java的加密算法
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        byte[] digest = md5.digest(str.getBytes());
        // 将字节数组转换为16进制字符串
        StringBuilder hexString = new StringBuilder();
        for(byte b:digest){
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        log.info("加密后的字符串:{}",hexString.toString());

        return hexString.toString();
    }
}
